package visao;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyVetoException;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 * TelaPrincipal.java
 * Created on May 3, 2009, 4:51:57 PM
 * @author thiago
 */
public class TelaPrincipal extends javax.swing.JFrame {

    private static TelaPrincipal instancia;

    /** Creates new form TelaPrincipal */
    public TelaPrincipal() {
        initComponents();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setLocationRelativeTo(null);
        TelaPrincipal.this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        ControladorDeEventos controlador = new ControladorDeEventos();
        btnFuncionarios.addActionListener(controlador);
        MenuSair.addMouseListener(controlador);
        instancia = this;
    }

    public static TelaPrincipal getInstancia() {
        return instancia;
    }

    public static void abrirJanela(JInternalFrame janela) {
        try {
            if (instancia != null) {
                instancia.desktop.add(janela);
                janela.setVisible(true);
                janela.setSelected(true);
            }
        } catch (PropertyVetoException ex) {
            ex.printStackTrace();
        }
    }

    /** 
     * Controlador estende de MouseAdapter para efetuar o tratamento do evento sem
     * precisar declarar todos os metodos da interface MouseListener
     * Se mais tarde for preciso faze-lo herdar de outra classe (o que e muito raro)
     * pode-se refatorar para outra abordagem
     */
    private class ControladorDeEventos extends MouseAdapter implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            Object origem = e.getSource();

            if (origem == btnFuncionarios) {
                abrirJanela(new ListaFuncionarios());
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getSource() == MenuSair) {
                System.exit(0);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jToolBar1 = new javax.swing.JToolBar();
        btnFuncionarios = new javax.swing.JButton();
        btnCapturarPonto = new javax.swing.JButton();
        desktop = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuCadastro = new javax.swing.JMenu();
        itemMenuFuncionario = new javax.swing.JMenuItem();
        itemMenuUsuario = new javax.swing.JMenuItem();
        MenuCapturar = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        itemMenuCapturar = new javax.swing.JMenuItem();
        MenuSair = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Controle de Ponto");
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setMinimumSize(new java.awt.Dimension(12, 10));
        jToolBar1.setPreferredSize(new java.awt.Dimension(100, 40));

        btnFuncionarios.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/professores.png"))); // NOI18N
        btnFuncionarios.setFocusable(false);
        btnFuncionarios.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnFuncionarios.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnFuncionarios);

        btnCapturarPonto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/materias.png"))); // NOI18N
        btnCapturarPonto.setFocusable(false);
        btnCapturarPonto.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCapturarPonto.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnCapturarPonto);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(1, 0, 0, 0);
        getContentPane().add(jToolBar1, gridBagConstraints);

        desktop.setBackground(new java.awt.Color(26, 98, 76));
        desktop.setMinimumSize(new java.awt.Dimension(300, 400));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(desktop, gridBagConstraints);

        menuCadastro.setMnemonic('C');
        menuCadastro.setText("Cadastro");

        itemMenuFuncionario.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        itemMenuFuncionario.setText("Funcionários");
        menuCadastro.add(itemMenuFuncionario);

        itemMenuUsuario.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        itemMenuUsuario.setText("Usuarios");
        menuCadastro.add(itemMenuUsuario);

        jMenuBar1.add(menuCadastro);

        MenuCapturar.setMnemonic('t');
        MenuCapturar.setText("Capturar");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, 0));
        jMenuItem1.setText("Capturar Ponto");
        MenuCapturar.add(jMenuItem1);

        itemMenuCapturar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        itemMenuCapturar.setText("Relatório");
        MenuCapturar.add(itemMenuCapturar);

        jMenuBar1.add(MenuCapturar);

        MenuSair.setText("Sair");
        jMenuBar1.add(MenuSair);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaPrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu MenuCapturar;
    private javax.swing.JMenu MenuSair;
    private javax.swing.JButton btnCapturarPonto;
    private javax.swing.JButton btnFuncionarios;
    private javax.swing.JDesktopPane desktop;
    private javax.swing.JMenuItem itemMenuCapturar;
    private javax.swing.JMenuItem itemMenuFuncionario;
    private javax.swing.JMenuItem itemMenuUsuario;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenu menuCadastro;
    // End of variables declaration//GEN-END:variables
}
